package com.ysyc.driver_ee.tools;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/6/7.
 */

public class LogUtils {
    private static final String TAG = "YSYC";
    private static final String HTTP = "HTTP";



    public static final String LINE_SEPARATOR = System.getProperty("line.separator");

    public static final boolean LOG_DEBUG = true;


    public static void d() {
        if (LOG_DEBUG) {
            Log.d(TAG, getLocation());
        }
    }

    public static void d(String msg) {
        if (LOG_DEBUG) {
            Log.d(TAG, getLocation() + msg);
        }
    }

    public static void i(String msg) {
        if (LOG_DEBUG) {
            Log.i(TAG, getLocation() + msg);
        }
    }

    public static void i() {
        if (LOG_DEBUG) {
           Log.i(TAG, getLocation());
        }
    }

    public static void e(String msg) {
        if (LOG_DEBUG) {
            Log.e(TAG, getLocation() + msg);
        }
    }

    public static void e(String msg,int i) {
        if (LOG_DEBUG) {
            Log.e(TAG, getLocation() + msg);
        }
    }

    public static void e(String msg, Throwable e) {
        if (LOG_DEBUG) {
            Log.e(HTTP, getLocation() + msg);
        }
    }

    public static void e(Throwable e) {
        if (LOG_DEBUG) {
            Log.e(TAG, getLocation(), e);
        }
    }

    public static void e() {
        if (LOG_DEBUG) {
            Log.e(TAG, getLocation());
        }
    }

    public static void printJson(String msg, String headString) {

        String message;
        try {
            if (msg.startsWith("{")) {
                JSONObject jsonObject = new JSONObject(msg);
                message = jsonObject.toString(4);//最重要的方法，就一行，返回格式化的json字符串，其中的数字4是缩进字符数
            } else if (msg.startsWith("[")) {
                JSONArray jsonArray = new JSONArray(msg);
                message = jsonArray.toString(4);
            } else {
                message = msg;
            }
        } catch (JSONException e) {
            message = msg;
        }

        printLine(true);
        message = "请求的地址是:" + headString + LINE_SEPARATOR + message;
        String[] lines = message.split(LINE_SEPARATOR);
        for (String line : lines) {
            e("║ " + line);
        }
        printLine(false);
    }

    public static void printLine(boolean isTop) {
        if (isTop) {
            e("╔═══════════════════════════════════════════════════════════════════════════════════════");
        } else {
           e("╚═══════════════════════════════════════════════════════════════════════════════════════");
        }
    }

    private static String getLocation() {
        final String className = LogUtils.class.getName();
        final StackTraceElement[] traces = Thread.currentThread()
                .getStackTrace();

        boolean found = false;

        for (StackTraceElement trace : traces) {
            try {
                if (found) {
                    if (!trace.getClassName().startsWith(className)) {
                        Class<?> clazz = Class.forName(trace.getClassName());
                        return "[" + getClassName(clazz) + ":"
                                + trace.getMethodName() + ":"
                                + trace.getLineNumber() + "]: ";
                    }
                } else if (trace.getClassName().startsWith(className)) {
                    found = true;
                }
            } catch (ClassNotFoundException ignored) {
            }
        }

        return "[]: ";
    }

    private static String getClassName(Class<?> clazz) {
        if (clazz != null) {
            if (!TextUtils.isEmpty(clazz.getSimpleName())) {
                return clazz.getSimpleName();
            }

            return getClassName(clazz.getEnclosingClass());
        }

        return "";
    }

}
